Skip to content

DSU with path halving#49

Merged
not522 merged 1 commit into
masterfrom
dsu_halving
Dec 12, 2020
Merged

DSU with path halving#49
not522 merged 1 commit into
masterfrom
dsu_halving

Conversation

@not522

@not522 not522 commented Dec 12, 2020

Copy link
Copy Markdown
Owner

resolve #28.

This PR introduces no-recursive DSU using path halving. I compared path compression, path splitting, and path halving. Runtimes of path splitting and path halving are shorter than the master's one, and they are almost the same. I selected path halving because of the simplicity of implementation.

Benchmark result (1,000,000 times merging)

--------------------------------------------------------------------------------------------- benchmark: 4 tests ---------------------------------------------------------------------------------------------
Name (time in ms)                        Min                   Max                  Mean             StdDev                Median                IQR            Outliers     OPS            Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_benchmark (0004_halving)       684.8580 (1.0)        712.0260 (1.00)       696.0139 (1.0)      12.2154 (2.68)       692.3994 (1.0)      21.8737 (3.08)          1;0  1.4368 (1.0)           5           1
test_benchmark (0003_splitti)       697.6434 (1.02)       708.4840 (1.0)        702.0563 (1.01)      4.5543 (1.0)        702.9542 (1.02)      7.1003 (1.0)           1;0  1.4244 (0.99)          5           1
test_benchmark (0001_master)      1,145.4572 (1.67)     1,182.5952 (1.67)     1,165.7987 (1.67)     18.0817 (3.97)     1,170.4463 (1.69)     35.0408 (4.94)          1;0  0.8578 (0.60)          5           1
test_benchmark (0002_compres)     1,149.8620 (1.68)     1,264.3050 (1.78)     1,193.5093 (1.71)     43.6715 (9.59)     1,180.0830 (1.70)     50.7904 (7.15)          1;0  0.8379 (0.58)          5           1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean

Result of AtCoder Library Practice Contest

Implementations

# master
def leader(self, a: int) -> int:
    assert 0 <= a < self._n

    if self.parent_or_size[a] < 0:
        return a

    self.parent_or_size[a] = self.leader(self.parent_or_size[a])
    return self.parent_or_size[a]

# path compression
def leader(self, a: int) -> int:
    assert 0 <= a < self._n

    leader = a
    while self.parent_or_size[leader] >= 0:
        leader = self.parent_or_size[leader]

    while self.parent_or_size[a] >= 0:
        self.parent_or_size[a], a = leader, self.parent_or_size[a]

    return leader

# path splitting
def leader(self, a: int) -> int:
    assert 0 <= a < self._n

    parent = self.parent_or_size[a]
    if parent < 0:
        return a

    while self.parent_or_size[parent] >= 0:
        self.parent_or_size[a], a, parent = (
            self.parent_or_size[parent],
            parent,
            self.parent_or_size[parent],
        )

    return parent

# path halving
def leader(self, a: int) -> int:
    assert 0 <= a < self._n

    parent = self.parent_or_size[a]
    while parent >= 0:
        if self.parent_or_size[parent] < 0:
            return parent
        self.parent_or_size[a], a, parent = (
            self.parent_or_size[parent],
            self.parent_or_size[parent],
            self.parent_or_size[self.parent_or_size[parent]]
        )

    return a

@not522 not522 merged commit 693b75a into master Dec 12, 2020
@not522 not522 deleted the dsu_halving branch December 12, 2020 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dsuを非再帰にする

1 participant